var app = getApp();
var pageSize = 20;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sum: "",
    record_data: [],
    conditions: {
      start_time: '',
      end_time: '',
    },
    page: 1,
    loadmore: false,
    nomore: false,
    empty: false,
    drawer: false,
    isFilter: false,
    contdetail: "",
    ispopshow: false,
  },


  onLoad: function (options) {
    this.accountRecordList();
  },


  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.accountRecordList();
    }
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },

  reloadData() {
    this.setData({
      record_data: [],
      page: 1,
      loadmore: false,
      nomore: false,
    })
    wx.showLoading();
    this.accountRecordList();
  },


  accountRecordList() {
    app.ajax({
      url: 'Salesman/Account/accountRecordList',
      method: "POST",
      data: {
        type: 2,
        page: this.data.page,
        row: pageSize,
        start_time: this.data.conditions.start_time,
        end_time: this.data.conditions.end_time,
        time_line: true
      },
      success: (res) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.data.code == 1000) {
          const source = res.data.data;
          const list = this.data.record_data;
          if (source.data.length > 0 && list.length > 0) {
            if (list[list.length - 1].date == source.data[0].date) {
              let last = list[list.length - 1].child.concat(source.data[0].child);
              list.pop();
              source.data[0].child = last;
            }
          }
          let lists = list.concat(source.data);
          let len = 0;
          lists.map(current => {
            len += current.child.length
          })
          console.log(len,"len",source.page.total,"source.page.total");
          this.setData({
            drawer: false,
            loadmore: len < source.page.total ? true : false,
            nomore: len >= source.page.total ? true : false,
            record_data: lists,
            empty: source.page.total == 0 ? true : false,
            sum: source.page.sum || 0
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        app.showToast("网络错误");
      }
    })
  },

  //查看省略文字
  lookmore(e) {
    var that = this;
    that.setData({
      contdetail: e.currentTarget.dataset.txt,
      ispopshow: true
    })
  },
  //关闭小黑窗户
  closeShow() {
    this.setData({
      ispopshow: false
    })
  },
  
  handleTime(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },

  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },


})